package libs.yzx.zaza;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Created by Administrator on 2016/7/12
 */
public class ViewPagerActivity extends AppCompatActivity {

    private TextView tv;
    private ViewPager vp;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_viewpager);

        tv = (TextView) findViewById(R.id.tv);
        vp = (ViewPager) findViewById(R.id.vp);

        final ImageView iv1 = new ImageView(this);
        final ImageView iv2 = new ImageView(this);
        final ImageView iv3 = new ImageView(this);
        iv1.setBackgroundColor(Color.RED);
        iv2.setBackgroundColor(Color.GREEN);
        iv3.setBackgroundColor(Color.BLUE);


        vp.setOffscreenPageLimit(3);
        vp.setAdapter(new PagerAdapter() {
            public int getCount() {
                return 3;
            }

            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            public Object instantiateItem(ViewGroup container, int position) {
                View view = null;
                if (position == 0)
                    view = iv1;
                if (position == 1)
                    view = iv2;
                if (position == 2)
                    view = iv3;
                container.addView(view);
                return view;
            }

            public void destroyItem(ViewGroup container, int position, Object object) {

            }
        });

        vp.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                tv.setText(position + "      " + positionOffset + "      " + positionOffsetPixels);
            }

            public void onPageScrollStateChanged(int state) {
            }

            public void onPageSelected(int position) {
            }
        });

    }


}
